package query;

import gui.Main;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;

import no.ntnu.fp.model.Person;

public class PersonQuery {
	private static ResultSet rs;

	/**
	 * Method for violently and Brutaly forcing a person into the database
	 * 
	 * @param per Person
	 *  - The Person you want to insert into the database
	 */
	public static void insertPerson(Person per){
		try{
			Statement stmt = Main.con.createStatement();
			String sql1 = "INSERT INTO Bruker(Navn, Passord, DateOfBirth";
			String sql2 = "VALUES('" + per.getName() + "', '" + per.getPassword() + "'," + per.getDateOfBirth().getTime();
			if(per.getEmail()!=null){
				sql1=sql1+", Epost";
				sql2=sql2+",'" + per.getEmail() + "'";
				}
			sql1 = sql1 + ")";
			sql2 = sql2 + ")";
			System.out.println(sql1+sql2);
			stmt.executeUpdate(sql1+sql2);
		}catch(SQLException e){
			e.printStackTrace();
		}
		
	}
	/**
	 * Method for getting a spesific person out of the db based on his name.
	 * 
	 * @param Name String
	 *  - The name of the person you want to find.
	 * @return Person
	 */

	public static Person getPerson(String Name){
		Person retur = new Person();
		try {
			Statement stmt = Main.con.createStatement();
			rs = stmt.executeQuery("SELECT * FROM Bruker WHERE Navn = '" + Name + "'");
			if(rs.next()){
				retur.setName(Name);
				retur.setEmail(rs.getString("Epost"));
				retur.setDateOfBirth(new Date(rs.getLong("DateOfBirth")));
				retur.setId(rs.getInt("BrukerID"));
				retur.setPassword(rs.getString("Passord"));
			}	
			//System.out.println( "+" + rs.getDate("DateOfBirth"));


		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}


		return retur;
	}
	/**
	 * Metode for getting a person out of the database
	 * 
	 * @param ID int
	 *  - The ID of the Person you wish to find.
	 * @return Person
	 *  - The Person that was find.
	 */
	public static Person getPerson(int ID){
		Person retur = new Person();
		try {
			Statement stmt = Main.con.createStatement();
			rs = stmt.executeQuery("SELECT * FROM Bruker WHERE BrukerID =   " + ID );
			if(rs.next()){
				retur.setName(rs.getString("Navn"));
				retur.setEmail(rs.getString("Epost"));
					retur.setDateOfBirth(new Date(rs.getLong("DateOfBirth")));
				retur.setId(rs.getInt("BrukerID"));
				retur.setPassword(rs.getString("Passord"));
			}	
			//System.out.println( "+" + rs.getDate("DateOfBirth"));


		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}


		return retur;
	}
	public static Person[] getAllPeople(){
		Person [] retur = null;
		Person per = new Person();
		ArrayList <Person> Ole = new ArrayList<Person>() ;
		try {
			Statement stmt = Main.con.createStatement();
			rs = stmt.executeQuery("SELECT * FROM Bruker");
			while(rs.next()){
				per = new Person();
				per.setName(rs.getString("Navn"));
				per.setEmail(rs.getString("Epost"));
				
					per.setDateOfBirth(new Date(rs.getLong("DateOfBirth")));
				
				per.setId(rs.getInt("BrukerID"));
				per.setPassword(rs.getString("Passord"));
				Ole.add(per);

			}

			retur = new Person[Ole.size()];
			

			for(int i=0; i<Ole.size();i++){
				retur[i] = Ole.get(i);
			}
			
			return retur;

		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}

	}


}
